package com.cloudcentral.config;

import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * Create by Administrator
 * 2023/11/24 15:41
 */
@Configuration
public class NacosConfig {
  @Bean
  public ConfigService configService(NacosConfigProperties nacosConfigProperties) throws NacosException {
    Properties properties = new Properties();
    properties.put("serverAddr", StringUtils.defaultIfBlank(nacosConfigProperties.getServerAddr(), ""));
    properties.put("username", StringUtils.defaultIfBlank(nacosConfigProperties.getUsername(), ""));
    properties.put("password", StringUtils.defaultIfBlank(nacosConfigProperties.getPassword(), ""));
    properties.put("namespace", StringUtils.defaultIfBlank(nacosConfigProperties.getNamespace(), ""));
    return NacosFactory.createConfigService(properties);
  }
}
